package com.wjd.parser;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 页面处理器
 *
 * @author weijiaduo
 * @since 2024/3/13
 */
public class PageHandler {

    /**
     * 中文字符正则表达式
     */
    private static final String REGEX = "[\u4E00-\u9FA5]";
    private static final Pattern PAT = Pattern.compile(REGEX);

    /**
     * 解析网页
     *
     * @param url 网页地址
     * @param doc 网页文档
     * @return 网页数据
     */
    public PageContent handle(String url, Document doc) {
        if (doc == null) {
            return null;
        }

        PageContent pageContent = new PageContent();
        String title = doc.title();
        String content = parseContent(doc);
        Set<String> links = parseLinks(doc);
        pageContent.setUrl(url);
        pageContent.setTitle(title);
        pageContent.setContent(content);
        pageContent.setLinks(links);
        pageContent.setOutDegree(links.size());
        return pageContent;
    }

    /**
     * 解析正文内容
     *
     * @param doc 网页文档
     * @return 正文内容
     */
    private String parseContent(Document doc) {
        StringBuilder sb = new StringBuilder();
        String webContent = doc.html();
        Matcher matcher = PAT.matcher(webContent);
        while (matcher.find()) {
            sb.append(matcher.group());
        }
        return sb.toString();
    }

    /**
     * 解析网页链接
     *
     * @param doc 网页文档
     * @return 网页链接
     */
    private Set<String> parseLinks(Document doc) {
        Set<String> links = new HashSet<>();
        Elements elements = doc.select("a[href]");
        for (Element link : elements) {
            String href = link.attr("abs:href");
            if (href.startsWith("http")) {
                links.add(href);
            }
        }
        return links;
    }

}
